<template>
    <el-dialog
            :title="!dataForm.orderId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="主键ID" prop="id">
                        <el-input v-model.number="dataForm.id" placeholder="主键ID"></el-input>
                </el-form-item>
                <el-form-item label="发电设备类型（如光伏组件、风力发电机等）" prop="equipmentType">
                        <el-input v-model="dataForm.equipmentType" placeholder="发电设备类型（如光伏组件、风力发电机等）"></el-input>
                </el-form-item>
                <el-form-item label="发电设备唯一标识" prop="equipmentId">
                        <el-input v-model="dataForm.equipmentId" placeholder="发电设备唯一标识"></el-input>
                </el-form-item>
                <el-form-item label="设备功率输出（瓦特）" prop="powerOutput">
                        <el-input v-model="dataForm.powerOutput" placeholder="设备功率输出（瓦特）"></el-input>
                </el-form-item>
                <el-form-item label="设备转速（转/分钟）" prop="rotationSpeed">
                        <el-input v-model="dataForm.rotationSpeed" placeholder="设备转速（转/分钟）"></el-input>
                </el-form-item>
                <el-form-item label="设备温度（摄氏度）" prop="temperature">
                        <el-input v-model="dataForm.temperature" placeholder="设备温度（摄氏度）"></el-input>
                </el-form-item>
                <el-form-item label="设备振动（加速度）" prop="vibration">
                        <el-input v-model="dataForm.vibration" placeholder="设备振动（加速度）"></el-input>
                </el-form-item>
                <el-form-item label="数据采集时间" prop="collectionTime">
                        <el-date-picker
                                v-model="dataForm.collectionTime"
                                type="datetime"
                                placeholder="数据采集时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="数据是否有效（经过校验）" prop="isValid">
                        <el-input v-model="dataForm.isValid" placeholder="数据是否有效（经过校验）"></el-input>
                </el-form-item>
                <el-form-item label="数据创建时间" prop="createdAt">
                        <el-date-picker
                                v-model="dataForm.createdAt"
                                type="datetime"
                                placeholder="数据创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="数据更新时间" prop="updatedAt">
                        <el-date-picker
                                v-model="dataForm.updatedAt"
                                type="datetime"
                                placeholder="数据更新时间">
                        </el-date-picker>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
        data () {
            return {
                visible: false,
                dataForm: {
                    id: null,
                    equipmentType: null,
                    equipmentId: null,
                    powerOutput: null,
                    rotationSpeed: null,
                    temperature: null,
                    vibration: null,
                    collectionTime: null,
                    isValid: null,
                    createdAt: null,
                    updatedAt: null,
                },
                dataRule: {
                    id: [
                        { required: true, message: '主键ID不能为空', trigger: 'blur' }
                    ],
                    equipmentType: [
                        { required: true, message: '发电设备类型（如光伏组件、风力发电机等）不能为空', trigger: 'blur' }
                    ],
                    equipmentId: [
                        { required: true, message: '发电设备唯一标识不能为空', trigger: 'blur' }
                    ],
                    powerOutput: [
                        { required: true, message: '设备功率输出（瓦特）不能为空', trigger: 'blur' }
                    ],
                    rotationSpeed: [
                        { required: true, message: '设备转速（转/分钟）不能为空', trigger: 'blur' }
                    ],
                    temperature: [
                        { required: true, message: '设备温度（摄氏度）不能为空', trigger: 'blur' }
                    ],
                    vibration: [
                        { required: true, message: '设备振动（加速度）不能为空', trigger: 'blur' }
                    ],
                    collectionTime: [
                        { required: true, message: '数据采集时间不能为空', trigger: 'blur' }
                    ],
                    isValid: [
                        { required: true, message: '数据是否有效（经过校验）不能为空', trigger: 'blur' }
                    ],
                    createdAt: [
                        { required: true, message: '数据创建时间不能为空', trigger: 'blur' }
                    ],
                    updatedAt: [
                        { required: true, message: '数据更新时间不能为空', trigger: 'blur' }
                    ],
                }
            }
        },
        methods: {
            init (id) {
                this.dataForm.orderId = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    this.dataForm.orderId = id || 0
                    if (this.dataForm.orderId) {
                        this.$http({
                            url: this.$http.adornUrl(`/app/AppGenerationEquipmentData/info/\\$\\{this.dataForm.orderId}`),
                            method: 'get',
                            params: this.$http.adornParams()
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.dataForm = data.appgenerationequipmentdata
                            }
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit () {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornUrl(`/app/AppGenerationEquipmentData/\\$\\{!this.dataForm.orderId ? 'save' : 'update'}`),
                            method: 'post',
                            data: this.$http.adornData(this.dataForm)
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refreshDataList')
                                    }
                                })
                            } else {
                                this.$message.error(data.msg)
                            }
                        })
                    }
                })
            }
        }
    }
</script>